<?php

namespace App\Models\Admin\Authority;

class AuthoritiesModel extends BaseAuthorityModel
{
    protected $table = 'authorities';

    const DEFAULT_ROUTER_DEFAULT = 'dashboard'; // 根父级id

    /**
     * 根据默认路由查询
     *
     * @param string $defaultRouter
     * @param array  $fields
     * @return array
     */
    public function getItemByDefaultRouter(string $defaultRouter, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
            ->where('default_router', $defaultRouter)
            ->where('deleted', self::DELETED_NO)
            ->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * 获取根角色列表
     *
     * @param array $fields
     * @return array
     */
    public function list(array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
            ->where('deleted', self::DELETED_NO)
            ->get()
            ->toArray();
    }

    /**
     * 根据父级id查询数据
     *
     * @param int   $parentId 父级id
     * @param array $fields
     * @return array
     */
    public function getItemByParentId(int $parentId, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
            ->where('parent_id', $parentId)
            ->where('deleted', self::DELETED_NO)
            ->first();
        return $result ? $result->toArray() : [];
    }
}
